package One;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Employee {
    /*
    创建员工类，属性有姓名、生日、入职时间、离职时间。生成get/set方法。
提供计算年龄的get方法。完成年龄的计算
提供计算工龄的get方法。完成工龄的计算
工龄计算规则如下：如果该员工在职，计算入职时间到当前时间的工龄。
如果该员工离职，计算入职时间到离职时间的工龄。
重写toString()方法，要求返回姓名、生日、工龄、年龄。
     */

    private String name;
    private String birthday;
    private String inTime;
    private String outTime;


    public Employee(String name, String birthday, String inTime, String outTime) {
        this.name = name;
        this.birthday = birthday;
        this.inTime = inTime;
        this.outTime = outTime;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getInTime() {
        return inTime;
    }

    public void setInTime(String inTime) {
        this.inTime = inTime;
    }

    public String getOutTime() {
        return outTime;
    }

    public void setOutTime(String outTime) {
        this.outTime = outTime;
    }



    public long getAge(String birth){
        LocalDate now = LocalDate.now();
        String[] array = birth.split("-");
        int birthYear = Integer.parseInt(array[0]);
        int birthMonth = Integer.parseInt(array[1]);
        int birthDay = Integer.parseInt(array[2]);
        LocalDate birthTime = LocalDate.of(birthYear,birthMonth,birthDay);
        return ChronoUnit.YEARS.between(birthTime,now);
    }

    public long workAge(String inTime,String outTime){
        String[] array = inTime.split("-");
        String[] outTimeArray = outTime.split("-");
        int inTimeYear = Integer.parseInt(array[0]);
        int inTimeMonth = Integer.parseInt(array[1]);
        int inTimeDay = Integer.parseInt(array[2]);
        LocalDate inJobTime = LocalDate.of(inTimeYear,inTimeMonth,inTimeDay);

        if (outTime!=""){
            int outTimeYear = Integer.parseInt(outTimeArray[0]);
            int outTimeMonth = Integer.parseInt(outTimeArray[1]);
            int outTimeDay = Integer.parseInt(outTimeArray[2]);

            LocalDate outJobTime = LocalDate.of(outTimeYear,outTimeMonth,outTimeDay);
            return ChronoUnit.YEARS.between(inJobTime,outJobTime);
        }else {
            return ChronoUnit.YEARS.between(inJobTime,LocalDate.now());
        }
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", birthday='" + birthday + '\'' +
                ", inTime='" + inTime + '\'' +
                ", outTime='" + outTime + '\'' +
                '}';
    }
}
